package org.wbd.pklib.db;

import javax.persistence.*;

import org.wbd.pklib.web.beans.BookBean;

import static javax.persistence.GenerationType.IDENTITY;
import java.util.HashSet;
import java.util.Set;

/**
 * Klasa reprezentująca obiekt ksiazki,obiekt tej klasy jest obiektowym odzwierciedleniem
 * rekord w tabeli "book"
 */
@Entity
@Table(name = "book")
public class Book implements java.io.Serializable {
	
	private String isbn;
	private String author;
	private String title;
	private String publisher;
	private Integer publicationYear;
	private String categories;
	private Set<Item> items = new HashSet<Item>(0);
	
	public Book() {
		isbn = "";
		author = "";
		title = "";
		publisher = "";
	}
	
	public Book(String isbn, String author, String title, String publisher,
			Integer publicationYear, String categories) {
		this.isbn = isbn;
		this.author = author;
		this.title = title;
		this.publisher = publisher;
		this.publicationYear = publicationYear;
		this.categories = categories;
	}
	
	public Book(String isbn, String author, String title, String publisher,
			Integer publicationYear, String categories,  Set<Item> items) {
		this.isbn = isbn;
		this.author = author;
		this.title = title;
		this.publisher = publisher;
		this.publicationYear = publicationYear;
		this.categories = categories;
		this.items = items;
	}


	@Id	
	@Column(name = "isbn", unique = true, nullable = false)
	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	@Column(name = "author", unique = false, nullable = false, length = 32)
	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	@Column(name = "title", unique = false, nullable = false, length = 64)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	@Column(name = "publisher", unique = false, nullable = false, length = 64)
	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	@Column(name = "publicationYear", unique = false, nullable = false, length = 4)
	public Integer getPublicationYear() {
		return publicationYear;
	}

	public void setPublicationYear(Integer publicationYear) {
		this.publicationYear = publicationYear;
	}
	
	
	@Column(name = "categories", unique = false, nullable = false)
	public String getCategories() {
		return categories;
	}

	public void setCategories(String categories) {
		this.categories = categories;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "itemJoin.book", cascade=CascadeType.ALL)
	public Set<Item> getItems() {
		return items;
	}

	public void setItems(Set<Item> items) {
		this.items = items;
	}
	
	public boolean equals(Object o) {
		
        if (this == o) return true;
        else if (this.isbn.compareTo(((Book)o).getIsbn()) == 0){
        	return true;
        }
        else {
        	
        	return false;
        }
    }

	@Override
	public String toString() {
		return "Book [isbn=" + isbn + ", author=" + author + ", title=" + title
				+ ", publisher=" + publisher + ", publicationYear="
				+ publicationYear + ", categories=" + categories + "]";
	}
	
	
}
